<?php
namespace Admin\Controller;

use Think\Controller;

class LoginController extends Controller
{
    public function index()
    {
        $this->display();
    }
    public function doLogin(){
    	$arr = ['status' => 1,'msg' => '登录失败！','data' => ''];
    	if(IS_AJAX){
            $userName = I('post.username/s','');
            $passWord = I('post.password/s','','strip_tags');
            if(empty($userName)){
                $arr['msg'] = '用户名不能为空！';
                exit(json_encode($arr));
            }
            $is_PMUM = preg_match('/^([A-Za-z0-9]+)$/',$userName);
            if(!$is_PMUM){
                $arr['msg'] = '用户名不合法！';
                exit(json_encode($arr));
            }
            if(empty($passWord)){
                $arr['msg'] = '密码不能为空！';
                exit(json_encode($arr));
            }

            $admin = M('admin');
            $adminInfo = $admin->field('id,name,pwd,salt,realName')->where(['status' => 1,'name' => $userName])->find();
            if(!$adminInfo){
                $arr['msg'] = '该用户不存在！';
                exit(json_encode($arr));
            }
            $pwd = md5($adminInfo['salt'].$passWord);
            if($pwd == $adminInfo['pwd'] ){
                $admin->execute('update dx_admin set loginNum = loginNum + 1 ,loginTime = "'.getDates().'", ip = "'.get_client_ip().'"where id = '.$adminInfo['id']);
            }else{
                $arr['msg'] = '密码错误';
                exit(json_encode($arr));
            }
            session('user',[
                'uid' => $adminInfo['id'],
                'account' => $adminInfo['name'],
                'realName' => $adminInfo['realName']
            ]);
            $arr['status'] = 0;
            $arr['msg'] = '登陆成功';
    	}
    	echo json_encode($arr);
    }
    public function loginOut(){
        $_SESSION = [];//此方法也可行
//        session(null);
        $this->redirect('login/index');
    }
}

